package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjljgxrrkljlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjljgxrrkljlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlCjljgxrrkljlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlCjljgxrrkljlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlCjljgxrrkljlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料坚固性、软弱颗粒含量试验检测记录详情Service业务层处理
 *
 * @author tll
 * @date 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjljgxrrkljlbBaDetailServiceImpl implements IReportJlCjljgxrrkljlbBaDetailService {

    private final ReportJlCjljgxrrkljlbBaDetailMapper baseMapper;

    /**
     * 查询粗集料坚固性、软弱颗粒含量试验检测记录详情
     */
    @Override
    public ReportJlCjljgxrrkljlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询粗集料坚固性、软弱颗粒含量试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportJlCjljgxrrkljlbBaDetailVo> queryPageList(ReportJlCjljgxrrkljlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjljgxrrkljlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料坚固性、软弱颗粒含量试验检测记录详情列表
     */
    @Override
    public List<ReportJlCjljgxrrkljlbBaDetailVo> queryList(ReportJlCjljgxrrkljlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjljgxrrkljlbBaDetail> buildQueryWrapper(ReportJlCjljgxrrkljlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjljgxrrkljlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlCjljgxrrkljlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getJiangxsygclj() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsygclj, bo.getJiangxsygclj());
        lqw.eq(bo.getJiangxsygljsysyqhgzl() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsygljsysyqhgzl, bo.getJiangxsygljsysyqhgzl());
        lqw.eq(bo.getJiangxsyhgljsyklhgzl() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsyhgljsyklhgzl, bo.getJiangxsyhgljsyklhgzl());
        lqw.eq(bo.getJiangxsygljfjzlssbfl() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsygljfjzlssbfl, bo.getJiangxsygljfjzlssbfl());
        lqw.eq(bo.getJiangxsygljfjzl() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsygljfjzl, bo.getJiangxsygljfjzl());
        lqw.eq(bo.getJiangxsysyzzl() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsysyzzl, bo.getJiangxsysyzzl());
        lqw.eq(bo.getJiangxsyssbfl() != null, ReportJlCjljgxrrkljlbBaDetail::getJiangxsyssbfl, bo.getJiangxsyssbfl());
        return lqw;
    }

    /**
     * 新增粗集料坚固性、软弱颗粒含量试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportJlCjljgxrrkljlbBaDetailBo bo) {
        ReportJlCjljgxrrkljlbBaDetail add = BeanUtil.toBean(bo, ReportJlCjljgxrrkljlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改粗集料坚固性、软弱颗粒含量试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportJlCjljgxrrkljlbBaDetailBo bo) {
        ReportJlCjljgxrrkljlbBaDetail update = BeanUtil.toBean(bo, ReportJlCjljgxrrkljlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjljgxrrkljlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料坚固性、软弱颗粒含量试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
